package com.sls.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.sls.mall.product.entity.AttrEntity;
import com.sls.mall.product.service.AttrAttrgroupRelationService;
import com.sls.mall.product.service.AttrService;
import com.sls.mall.product.service.CategoryService;
import com.sls.mall.product.vo.AttrGroupRelationVO;
import com.sls.mall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sls.mall.product.entity.AttrGroupEntity;
import com.sls.mall.product.service.AttrGroupService;
import com.sls.mall.common.utils.PageUtils;
import com.sls.mall.common.utils.R;



/**
 * 属性分组
 *
 * @author tg
 * @email tg@gmail.com
 * @date 2021-04-24 16:14:45
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOS)
    {
        attrAttrgroupRelationService.saveBatch(attrGroupRelationVOS);

        return R.ok();
    }

    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId)
    {

        //1. 查出当前分类下的所有属性分组
        //2. 查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",attrGroupWithAttrsVOS);
    }

    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId)
    {

        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",attrEntities);
    }

    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId,
                          @RequestParam Map<String,Object> params)
    {

        PageUtils pageUtils = attrService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page",pageUtils);
    }


    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] attrGroupRelationVO)
    {
        attrService.deleteRelation(attrGroupRelationVO);
        return R.ok();
    }

    /**
     *  按分类id 查询列表
     * @param params
     * @param catelogId
     * @return
     */
    @RequestMapping("/list/{catelogId}")
    public R listByCatelogIg(@RequestParam Map<String,Object> params,@PathVariable("catelogId")Long catelogId)
    {
        PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page",pageUtils);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		categoryService.findCatelogPath(attrGroup.getCatelogId());

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
